package com.nuc.kl.module.basedata.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.base.BaseAction;
import com.nuc.kl.module.basedata.entity.Navigation;

@Controller
@Scope("prototype")
public class NavigationAction extends BaseAction<Navigation> {

	private Long parentId;

	public String getById() throws Exception {
		List list = new ArrayList();
		Navigation navigation = navigationService.getById(model.getId());
		list.add(navigation);
		String[] strs = { "parent", "children" };
		listToJson(list, list.size(), strs);

		return null;
	}

	private void listToJson(List list, int size, String[] strs) {
		// TODO Auto-generated method stub
		
	}

	/** 列表 */
	public String listJson() throws Exception {

		List<Navigation> list = navigationService.findList(model.getId());

		/*
		 * for (Navigation nav : list) { //
		 * if(!navigationService.findList(nav.getId()).isEmpty()){ //
		 * nav.setState("closed"); // } nav.setChildren(null);
		 * nav.setParent(null); }
		 * 
		 * response.setContentType("text/html;charset=utf-8");
		 * response.getWriter().write(JSONArray.fromObject(list).toString());
		 * return null;
		 */

		// int total = list.size();
		// StringBuffer json = new StringBuffer().append("{'total':"+ total +
		// ",'rows':[");
		StringBuffer json = new StringBuffer("[");
		for (Navigation nav : list) {

			String parentId = nav.getParent() == null ? null : (nav.getParent()
					.getId() + "").trim();
			String creator = nav.getCreator() == null ? "" : nav.getCreator()
					.getName();
			String createTime = nav.getCreateTime() == null ? "" : (nav
					.getCreateTime() + "").trim();
			String description = nav.getDescription() == null ? "" : nav
					.getDescription();
			String state = nav.getState() == null ? "open" : nav.getState();

			json.append("{\"id\":" + nav.getId() + ",\"name\":\""
					+ nav.getName() + "\"" + ",\"description\":\""
					+ description + "\"" + ",\"_parentId\":\"" + parentId
					+ "\"" + ",\"state\":\"" + state + "\""
					+ ",\"createTime\":\"" + createTime + "\""
					+ ",\"creator\":\"" + creator + "\"},");
		}

		String jsonString = json.toString().substring(0, json.length() - 1)
				+ "]";
		// System.out.println(jsonString);

		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(jsonString);

		return null;

	}

	/** 列表 */
	public String listJsonLeft() throws Exception {
		
		List<Navigation> list = navigationService.findList(model.getId());
		
		StringBuffer json = new StringBuffer("[");
		for (Navigation nav : list) {
			
			String parentId = nav.getParent() == null ?null:(nav.getParent().getId()+"").trim();
			String state = nav.getState() == null ?"open":nav.getState();
			String url = nav.getUrl() == null ?null:nav.getUrl();
			
			json.append("{\"id\":" + nav.getId() 
						+ ",\"text\":\"" + nav.getName() + "\"" 
						+ ",\"parent_id\":" + parentId
						+ ",\"state\":\"" + state + "\"" 
						+ ",\"attributes\":{\"url\":\"" + url + "\"}},");
		}
		
		String jsonString = json.toString().substring(0, json.length()-1) + "]";
//		System.out.println(jsonString);
		
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(jsonString);		
		
		return null;
		
		
	}

	/** 列表 */
	public String list() throws Exception {
		return "list";
	}

	/** 删除 */
	public String delete() throws Exception {
		// 配置 了级联:删除：children
		List<Navigation> cList = navigationService.findList(model.getId());
		int size = cList.size();
		if (size > 0) {
			for (Navigation cnav : cList) {
				navigationService.delete(cnav.getId());
			}
		}
		navigationService.delete(model.getId());

		return "toList";
	}

	/** 添加 */
	public String add() throws Exception {
		model.setCreateTime(new Date());
		// TODO 从session里去user
		model.setCreator(userService.getById(1L));
		model.setParent(navigationService.getById(parentId));
		navigationService.save(model);

		Navigation navigation = navigationService.getById(parentId);
		navigation.setState("closed");
		navigationService.update(navigation);

		return "toList";
	}

	/** 修改 */
	public String edit() throws Exception {

		Navigation nav = navigationService.getById(model.getId());
		nav.setName(model.getName());
		nav.setDescription(model.getDescription());
		nav.setCreateTime(new Date());

		navigationService.update(nav);
		return null;
	}

	// -----------------getter-----------setter---------------------------

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

}
